Partial Class pages_Datas_Data : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property DataId() As Integer
        Get
            Return CWeb.RequestInt("dataId")
        End Get
    End Property
    Public ReadOnly Property SeriesId() As Integer
        Get
            Return CWeb.RequestInt("seriesId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return DataId <> Integer.MinValue
        End Get
    End Property
    Public ReadOnly Property RequestColumns() As List(Of Integer)
        Get
            Return CWeb.RequestInts("columns")
        End Get
    End Property
    Public ReadOnly Property RequestDecisions() As List(Of Integer)
        Get
            Return CWeb.RequestInts("decisions")
        End Get
    End Property
#End Region

#Region "Members"
    Private m_data As CData
#End Region

#Region "Data"
    Public ReadOnly Property Series() As CSeries
        Get
            Return IIf(IsEdit, Data.Series, CSeries.Cache.GetById(SeriesId))
        End Get
    End Property
    Public ReadOnly Property [Data]() As CData
        Get
            If IsNothing(m_data) Then
                If IsEdit Then
                    m_data = CData.Cache.GetById(DataId)
                    If IsNothing(m_data) Then CSitemap.RecordNotFound("Data", DataId)
                Else
                    m_data = New CData
                    m_data.DataSeriesId = SeriesId
                    If Series.Datas.Count > 0 Then
                        m_data.DataDate = Series.Datas.MaxDate.AddDays(1)
                    End If
                End If
            End If
            Return m_data
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.DataEdit(Me.Data.DataId, RequestColumns, RequestDecisions))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Datas(SeriesId, RequestColumns, RequestDecisions))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Init
        txtSeries.Text = Series.SeriesName
        txtSeries.NavigateUrl = CSitemap.Datas(SeriesId, RequestColumns, RequestDecisions)

        If IsEdit Then            
            Me.Title = "Data Details"
        Else
            Me.Title = "Add A New Data"
        End If

        btnDelete.Visible = IsEdit
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Data"
    End Sub
    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostback Then Exit Sub

        LoadData()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveData()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.Data.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadData()
        With Me.Data
            txtDataDate.ValueDate = .DataDate
            txtDataOpen.ValueDbl =  .DataOpen
            txtDataHigh.ValueDbl =  .DataHigh
            txtDataLow.ValueDbl =  .DataLow
            txtDataClose.ValueDbl =  .DataClose
        End With
    End Sub
    Protected Sub SaveData()
        With Me.Data
            .DataDate = txtDataDate.ValueDate
            .DataOpen = txtDataOpen.ValueDbl
            .DataHigh = txtDataHigh.ValueDbl
            .DataLow = txtDataLow.ValueDbl
            .DataClose = txtDataClose.ValueDbl

            .Save()

            Series.Datas.DoCalcs()
        End With
    End Sub
#End Region


End Class
